package cn.doways.api.utils;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalUnit;
import java.time.temporal.WeekFields;
import java.util.Date;

/**
 * @ClassName DateUtils
 * @Description //TODO
 * @Author shellming
 * @Date 2019-08-27 01:02
 **/
public class DateUtils {

    public static Date asDate(LocalDate localDate) {
        return Date.from(localDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant());
    }

    public static Date asDate(LocalDateTime localDateTime) {
        return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
    }

    public static LocalDate asLocalDate(Date date) {
        return Instant.ofEpochMilli(date.getTime()).atZone(ZoneId.systemDefault()).toLocalDate();
    }

    public static LocalDateTime asLocalDateTime(Date date) {
        return Instant.ofEpochMilli(date.getTime()).atZone(ZoneId.systemDefault()).toLocalDateTime();
    }

    public static Long getMilliByTime(LocalDateTime time) {
        /*
         @Description: 获取指定日期的毫秒
        * @Param: [time]
        * @return: java.lang.Long
        * @Author: shellming
        * @Date: 2019-08-28
        */
        return time.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
    }


    public static Long getSecondsByTime(LocalDateTime time) {
        /*
         @Description: 获取指定日期的秒
        * @Param: [time]
        * @return: java.lang.Long
        * @Author: shellming
        * @Date: 2019-08-28
        */
        return time.atZone(ZoneId.systemDefault()).toInstant().getEpochSecond();
    }


    public static String formatTime(LocalDateTime time,String pattern) {
        /*
         @Description: 获取指定时间的指定格式
        * @Param: [time, pattern]
        * @return: java.lang.String
        * @Author: shellming
        * @Date: 2019-08-28
        */
        return time.format(DateTimeFormatter.ofPattern(pattern));
    }


    public static String formatNow(String pattern) {
        /*
         @Description: 获取当前时间的指定格式
        * @Param: [pattern]
        * @return: java.lang.String
        * @Author: shellming
        * @Date: 2019-08-28
        */
        return  formatTime(LocalDateTime.now(), pattern);
    }


    public static LocalDateTime plus(LocalDateTime time, long number, TemporalUnit field) {

        /*
         @Description: 日期加上一个数,根据field不同加不同值,field为ChronoUnit.*
        * @Param: [time, number, field]
        * @return: java.time.LocalDateTime
        * @Author: shellming
        * @Date: 2019-08-28
        */
        return time.plus(number, field);
    }


    public static LocalDateTime minu(LocalDateTime time, long number, TemporalUnit field){

        /*
         @Description: 日期减去一个数,根据field不同减不同值,field参数为ChronoUnit.*
        * @Param: [time, number, field]
        * @return: java.time.LocalDateTime
        * @Author: shellming
        * @Date: 2019-08-28
        */
        return time.minus(number,field);
    }


    public static long betweenTwoTime(LocalDateTime startTime, LocalDateTime endTime, ChronoUnit field) {

        /*
         @Description: 获取两个日期的差  field参数为ChronoUnit.*
        * @Param: [startTime, endTime, field]
        * @return: long
        * @Author: shellming
        * @Date: 2019-08-28
        */
        Period period = Period.between(LocalDate.from(startTime), LocalDate.from(endTime));
        if (field == ChronoUnit.YEARS){
            return period.getYears();
        }
        if (field == ChronoUnit.MONTHS){
            return period.getYears() * 12 + period.getMonths();
        }
        return field.between(startTime, endTime);
    }


    public static LocalDateTime getDayStart(LocalDateTime time) {
        /*
         @Description: 获取一天的开始时间，2017,7,22 00:00
        * @Param: [time]
        * @return: java.time.LocalDateTime
        * @Author: shellming
        * @Date: 2019-08-28
        */
        return time.withHour(0)
                .withMinute(0)
                .withSecond(0)
                .withNano(0);
    }


    public static LocalDateTime getDayEnd(LocalDateTime time) {
        /* 获取一天的结束时间，2017,7,22 23:59:59.999999999
         @Description:
        * @Param: [time]
        * @return: java.time.LocalDateTime
        * @Author: shellming
        * @Date: 2019-08-28
        */
        return time.withHour(23)
                .withMinute(59)
                .withSecond(59)
                .withNano(999999999);
    }

    public static int getJustWeek(LocalDateTime start,LocalDateTime end){
        WeekFields weekFields=WeekFields.of(DayOfWeek.SUNDAY,7);
        return end.get(weekFields.weekOfYear())-start.get(weekFields.weekOfYear());
    }
    public static int getJustWeekDate(LocalDate start,LocalDate end){
        WeekFields weekFields=WeekFields.of(DayOfWeek.SUNDAY,7);
        return end.get(weekFields.weekOfYear())-start.get(weekFields.weekOfYear());
    }

    public static LocalTime getLocalTime(Date date){
        String s=new String();

        Instant instant = date.toInstant();
        ZoneId zoneId=ZoneId.systemDefault();
        LocalDateTime localDateTime=LocalDateTime.ofInstant(instant,zoneId);
        return localDateTime.toLocalTime();
    }


}
